import { _decorator, Component, Animation, Sprite, UITransform, AnimationComponent } from 'cc'
import { DataCenter } from '../Runtime/DataCenter'
import { StateMachine } from './StateMachine'
const { ccclass, property } = _decorator

@ccclass('Role')
export class Role extends Component {
  //人物当前的位置
  x: number = 0
  y: number = 0

  //角色动画组件
  _animationComponent: AnimationComponent = null

  //创建人物精灵
  createRoleSprite() {
    const sprite = this.addComponent(Sprite)
    sprite.sizeMode = Sprite.SizeMode.CUSTOM
    const transform = this.node.getComponent(UITransform)
    transform.setContentSize(DataCenter.TILE_WIDTH * 4, DataCenter.TILE_HEIGHT * 4)
    this.node.setPosition(
      this.x * DataCenter.TILE_WIDTH - DataCenter.TILE_WIDTH * 1.5,
      -(this.y * DataCenter.TILE_HEIGHT - DataCenter.TILE_HEIGHT * 1.5),
    )
    this.node.addComponent(Animation)
    this._animationComponent = this.getComponent(Animation)
  }

  setPos(x: number, y: number) {
    this.x = x
    this.y = y
    this.node.setPosition(
      this.x * DataCenter.TILE_WIDTH - DataCenter.TILE_WIDTH * 0.5,
      -(this.y * DataCenter.TILE_HEIGHT - DataCenter.TILE_HEIGHT * 0.5),
    )
  }
}
